package com.wfs.myapp.repository.bk;

import com.wfs.myapp.entity.domain.AiTalk;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface AiTalkRepository extends JpaRepository<AiTalk, Long> {

    @Query(nativeQuery = true, value = " SELECT * FROM ai_talk ait " +
            " ORDER BY ait.created_time desc " +
            " limit 0,1 ")
    AiTalk findLastTimeByUserId(Long userId);


    List<AiTalk> findBySessionIdOrderByCreatedTime(String sessionId);

    @Query(nativeQuery = true, value = " select * from ai_talk " +
            " where session_id = " +
            "   (SELECT session_id FROM ai_talk ait " +
            "    WHERE ait.user_id = :userId " +
            "    AND ait.created_time >= :sessionTime " +
            "    ORDER BY ait.created_time desc " +
            "    limit 0, 1" +
            "   ) " +
            " ORDER BY created_time "
    )
    List<AiTalk> findByUserIdAndCreatedTime(Long userId, LocalDateTime sessionTime);


}




